package com.ouyangtao.spark.session;

import org.apache.spark.Accumulator;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;

import java.util.Arrays;
import java.util.List;

/**
 * @author ouyangtao
 */
public class AccumulatorTest2 {


    public static void main(String[]args) {
        SparkConf conf = new SparkConf().setAppName("AccumulatorDemo").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);

       final Accumulator<Integer> acc = sc.accumulator(100, new AccumulatorTest());

        List<Integer> seq = Arrays.asList(1, 2, 3, 4);
        JavaRDD<Integer> rdd = sc.parallelize(seq);

        rdd.foreach(new VoidFunction<Integer>() {

            @Override
            public void call(Integer arg0) throws Exception {
                acc.add(arg0);
            }

        });

        System.out.println(acc.value());
        ;
    }
}
